<?php
/**
 *
 * 帖子的评论
 *
 * SearchTopicComment.class.php
 * Created by IntelliJ IDEA.
 *
 * @author: hjh <hjh@jf.com>
 *
 * Date: 2017/10/28
 * Time: 08:28
 */

namespace Search;

use Circle\Comment;

class SearchTopicComment extends SearchBase
{
    public static $QUEUE_NAME = 'SearchTopicComment';
    public static $SEARCH_INDEX = self::TOPIC_COMMENT_SEARCH;

    const FIELD_COMMENT_ID = 'commentid';
    const FIELD_ITEM_ID = 'itemid';
    const FIELD_USER_ID = 'userid';
    const FIELD_REPLY_ID = 'replyid';
    const FIELD_IS_GOOD = 'isgood';
    const FIELD_AT_USER_ID = 'atuserid';
    const FIELD_CONTENT = 'content';
    const FIELD_PLATFORM = 'platform';
    const FIELD_STATUS = 'status';
    const FIELD_FLOOR_NUM = 'floornum';
    const FIELD_LIKE_COUNT = 'likecount';
    const FIELD_SUB_COMMENT_CNT = 'subcommentcnt';
    const FIELD_CREATE_TIME = 'createtime';
    const FIELD_HOT_SCORE = 'hotscore';

    const SORT_FIELD_HOT_SCORE = self::FIELD_HOT_SCORE;
    const SORT_FIELD_CREATE_TIME = self::FIELD_CREATE_TIME;

    public static $SEARCH_FIELD_MAP = [
        Comment::FIELD_COMMENT_ID => [self::FIELD_COMMENT_ID, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_ITEM_ID => [self::FIELD_ITEM_ID, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_USER_ID => [self::FIELD_USER_ID, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_REPLY_ID => [self::FIELD_REPLY_ID, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_IS_GOOD => [self::FIELD_IS_GOOD, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_AT_USER_ID => [self::FIELD_AT_USER_ID, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_CONTENT => [self::FIELD_CONTENT, self::FIELD_DATA_TYPE_STRING],
        Comment::FIELD_PLATFORM => [self::FIELD_PLATFORM, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_STATUS => [self::FIELD_STATUS, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_FLOOR_NUM => [self::FIELD_FLOOR_NUM, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_LIKE_COUNT => [self::FIELD_LIKE_COUNT, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_SUB_COMMENT_CNT => [self::FIELD_SUB_COMMENT_CNT, self::FIELD_DATA_TYPE_INT],
        Comment::FIELD_CREATE_TIME => [self::FIELD_CREATE_TIME, self::FIELD_DATA_TYPE_DATE],
        //Comment::FIELD_HOT_SCORE => [self::FIELD_HOT_SCORE, self::FIELD_DATA_TYPE_DOUBLE], //搜索的时候直接添加
    ];

    /**
     * @see SearchBase::Add($id, $info)     新增
     * @see SearchBase::update($id, $info)  更新
     */

    /**
     * 获取热门帖子
     *
     * @param int   $topicId 帖子Id
     * @param int   $limit   返回条数
     * @param array $fields  返回的字段
     * @return array
     */
    public static function searchHotComments($topicId, $limit, $fields = [])
    {
        if (empty($fields)) {
            $fields = self::buildAllFields();
        }
        $query = [
            "query" => [
                "bool" => [
                    "must" => [
                        [
                            "term" => [
                                self::FIELD_ITEM_ID => $topicId,
                            ]
                        ],
                        [
                            "term" => [
                                self::FIELD_REPLY_ID => 0
                            ]
                        ],
                        [
                            "bool" => [
                                "should" => [
                                    [
                                        "range" => [
                                            self::FIELD_LIKE_COUNT => [
                                                "gt" => 1,
                                                "boost" => 1
                                            ]
                                        ]
                                    ],
                                    [
                                        "range" => [
                                            self::FIELD_SUB_COMMENT_CNT => [
                                                "gt" => 0,
                                                "boost" => 2
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ],
                ]
            ],
            "sort" => [
                "_score" => [
                    "order" => self::SORT_TYPE_DESC
                ],
            ],
            "_source" => $fields,
            "from" => 0,
            "size" => $limit,
            "min_score" => 1
        ];

        return self::getSearchResults($query, $fields);
    }

    /**
     * 搜索评论
     *
     * @param int   $topicId           帖子Id
     * @param int   $userId            用户Id, 只看楼主的话 需要传入UserId
     * @param array $excludeCommentIds 排除评论ID
     * @param int   $offset            分页起始值
     * @param int   $limit             每页数据大小
     * @param array $fields            返回字段
     * @param array $sort              排序，默认按评论时间倒序
     * @return array
     */
    public static function searchComments($topicId, $userId = 0, $excludeCommentIds = [], $offset = 0, $limit = 10, $fields = [], $sort = [])
    {
        if (empty($fields)) {
            $fields = self::buildAllFields();
        }
        if (empty($sort)) {
            $sort = [
                self::SORT_FIELD_CREATE_TIME => [
                    'order' => self::SORT_TYPE_DESC
                ]
            ];
        }
        $must = [
            [
                "term" => [
                    self::FIELD_ITEM_ID => $topicId,
                ]
            ],
            [
                "term" => [
                    self::FIELD_REPLY_ID => 0
                ]
            ]
        ];
        if ($userId > 0) {
            $must[] = [
                "term" => [
                    self::FIELD_USER_ID => $userId
                ]
            ];
        }
        $query = [
            "query" => [
                "bool" => [
                    "must" => $must
                ]
            ],
            "sort" => $sort,
            "_source" => $fields,
            "from" => $offset,
            "size" => $limit
        ];
        if (count($excludeCommentIds) > 0) {
            $query["query"]["bool"]["must_not"] = [
                "terms" => [
                    self::FIELD_COMMENT_ID => $excludeCommentIds
                ]
            ];
        }

        return self::getSearchResults($query, $fields);
    }

    /**
     * 获取子评论
     *
     * @param       $topicId
     * @param array $replyIds
     * @param int   $offset
     * @param int   $limit
     * @param array $fields
     * @param array $sort
     * @return array
     */
    public static function searchSubComments($topicId, $replyIds = [], $offset = 0, $limit = 20, $fields = [], $sort = [])
    {
        if (empty($fields)) {
            $fields = self::buildAllFields();
        }
        if (empty($sort)) {
            $sort = [
                self::SORT_FIELD_CREATE_TIME => [
                    'order' => self::SORT_TYPE_DESC
                ]
            ];
        }
        $query = [
            "query" => [
                "bool" => [
                    "must" => [
                        [
                            "term" => [
                                self::FIELD_STATUS => Comment::STATUS_NORMAL
                            ]
                        ],
                        [
                            "term" => [
                                self::FIELD_ITEM_ID => $topicId,
                            ]
                        ],
                        [
                            "terms" => [
                                self::FIELD_REPLY_ID => $replyIds
                            ]
                        ]
                    ]
                ]
            ],
            "sort" => $sort,
            "_source" => $fields,
            "from" => $offset,
            "size" => $limit
        ];

        return self::getSearchResults($query, $fields);
    }

    /**
     * 构造全部返回字段
     *
     * @return array
     */
    protected static function buildAllFields()
    {
        return [
            self::FIELD_COMMENT_ID,
            self::FIELD_ITEM_ID,
            self::FIELD_USER_ID,
            self::FIELD_REPLY_ID,
            self::FIELD_IS_GOOD,
            self::FIELD_AT_USER_ID,
            self::FIELD_CONTENT,
            self::FIELD_PLATFORM,
            self::FIELD_STATUS,
            self::FIELD_FLOOR_NUM,
            self::FIELD_LIKE_COUNT,
            self::FIELD_SUB_COMMENT_CNT,
            self::FIELD_CREATE_TIME,
            self::FIELD_HOT_SCORE,
        ];
    }
}
